﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data
{
    public partial class FrmDistributor : Form
    {

        DataBase userDB = new DataBase();

        public FrmDistributor()
        {
            InitializeComponent();

            //统一页面窗口的属性，例如窗口打开都是页面最中间
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }

        /// <summary>
        /// 初始化页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmDistributor_Load(object sender, EventArgs e)
        {
            bindGridData();
            bindComboxData();
        }

        /// <summary>
        /// 初始化页面数据绑定
        /// </summary>
        private void bindGridData()
        {
            try
            {
                string sqlstr = "select * from S_distributor";
                if (textBox1.Text != "")
                {
                    sqlstr += " where Dis_code='" + textBox1.Text.Trim() + "'";
                }
                DataTable table = userDB.show(sqlstr);
                this.dgvDistributors.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                userDB.conn_close();
            }

        }

        /// <summary>
        /// 初始化页面数据绑定
        /// </summary>
        private void bindComboxData()
        {
            try
            {
                string sqlstr = "select * from S_area";
                DataTable table = userDB.show(sqlstr);
                this.comboBox1.ValueMember = "Area_code";
                this.comboBox1.DisplayMember = "Area_name";
                this.comboBox1.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                userDB.conn_close();
            }

        }

        /// <summary>
        /// 编辑按钮事件（根据Dis_Code是否存在来判断新增还是修改）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string Dis_code = textBox1.Text.Trim();
                string Dis_name = textBox2.Text.Trim();
                string Dis_address = textBox3.Text.Trim();
                string Dis_phoneint = textBox4.Text.Trim();
                string Dis_manger = textBox5.Text.Trim();
                string Dis_area = comboBox1.SelectedValue.ToString();

                //查询code是否存在
                string sqlExists = string.Format("select * from S_distributor where Dis_code='{0}'", Dis_code);
                DataSet ds = userDB.getDs(sqlExists, "S_distributor");

                //如果能查询到数据，则为编辑，否则新增

                if (ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DialogResult dialogClose = MessageBox.Show("你确定要修改吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogClose == DialogResult.OK)
                    {
                        string sqlstr = string.Format("update S_distributor set Dis_name='{0}',Dis_address='{1}',Dis_phone='{2}',Dis_manger='{3}',Area_code='{4}' where Dis_code='{5}'",
                       Dis_name, Dis_address, Dis_phoneint, Dis_manger, Dis_area, Dis_code);
                        userDB.Distributortableinsert(sqlstr);
                        MessageBox.Show("修改成功！");
                    }

                }
                else
                {
                    DialogResult dialogClose1 = MessageBox.Show("你确定要新增吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogClose1 == DialogResult.OK)
                    {
                        string sqlstr = "insert into S_distributor(Dis_code,Dis_name,Dis_address ,Dis_phone,Dis_manger,Area_code) values('" + Dis_code + "','" + Dis_name + "','" + Dis_address + "','" + Dis_phoneint + "','" + Dis_manger + "', '" + Dis_area + "')";
                        userDB.Distributortableinsert(sqlstr);
                        MessageBox.Show("新增成功！");
                    }

                }

                bindGridData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据插入错误" + ex.Message);
            }
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dialogClose = MessageBox.Show("你确定要删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dialogClose == DialogResult.OK)
            {
                try
                {
                    string Dis_code = textBox1.Text.Trim();
                    string delete = "DELETE FROM S_distributor WHERE Dis_code ='" + Dis_code + "'";
                    userDB.Distributortabledelete(delete);
                    ClearText();
                    bindGridData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据删除错误" + ex.Message);
                }
            }
        }

        /// <summary>
        /// 选择列表行数据，自动绑定右侧编辑区域内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvDistributors_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDistributors.SelectedRows.Count == 0)
            {
                return;
            }
            DataGridViewRow currentRow = dgvDistributors.CurrentRow;
            this.textBox1.Text = currentRow.Cells[0].Value.ToString();
            this.textBox2.Text = currentRow.Cells[1].Value.ToString();
            this.textBox3.Text = currentRow.Cells[2].Value.ToString();
            this.textBox4.Text = currentRow.Cells[3].Value.ToString();
            this.textBox5.Text = currentRow.Cells[4].Value.ToString();
        }

        /// <summary>
        /// 查询方法，根据Dis_code查询分销商
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            bindGridData();

        }

        private void ClearText() {
            this.textBox1.Text = "";
            this.textBox2.Text = "";
            this.textBox3.Text = "";
            this.textBox4.Text = "";
            this.textBox5.Text = "";
        }
    }

}
